/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.service.services.views.orgchart;

import java.awt.Graphics;

import javax.swing.JPanel;


/**
 * This class implements an organization chart sub-panel.
 * This is merely a JPanel which draws an horizontal line
 * between a cell panel located in a vertical box and
 * another cell panel located in a vertical box.
 * These panels should be the first and the last sons
 * of the father cell.
 */
public class XJPanel extends JPanel
{
    /** Coordinate of line beginning. */
    private int x1 = 0;

    /** Coordinate of line end. */
    private int x2 = 0;

    /** First sibling of current level cells. */
    private JPanel p1;

    /** Last siblings of current level cells. */
    private JPanel p2;

    /** First enclosing vertical panel. */
    private JPanel v1;

    /** Last enclosing vertical panel. */
    private JPanel v2;

    /**
     * Sets first sibling of current level cells.
     *
     * @param pan  Corresponding panel.
     */
    public final void setP1(final JPanel pan)
    {
        p1 = pan;
    }

    /**
     * Sets last siblings of current level cells.
     *
     * @param pan  Corresponding panel.
     */
    public final void setP2(final JPanel pan)
    {
        p2 = pan;
    }

    /**
     * Sets first enclosing vertical panel.
     *
     * @param pan  Corresponding panel.
     */
    public final void setV2(final JPanel pan)
    {
        v2 = pan;
    }

    /**
     * Sets last enclosing vertical panel.
     *
     * @param pan  Corresponding panel.
     */
    public final void setV1(final JPanel pan)
    {
        v1 = pan;
    }

    /**
     * Computes the line coordinates.
     */
    public final void recalcule()
    {
        //DW/2573/BeginPatch
        if ((OrgChartPanel.getDirection() == OrgChartPanel.HORIZONTAL) ||
                (OrgChartPanel.getDirection() == OrgChartPanel.SEMALIKE))//DW/2573/EndPatch
        {
            x1 = v1.getBounds().x + p1.getBounds().x +
                (int) (p1.getPreferredSize().getWidth() / 2);
            x2 = v2.getBounds().x + p2.getBounds().x +
                (int) (p2.getPreferredSize().getWidth() / 2);
        }
        else
        {
            x1 = v1.getBounds().y + p1.getBounds().y +
                (int) (p1.getPreferredSize().getHeight() / 2);
            x2 = v2.getBounds().y + p2.getBounds().y +
                (int) (p2.getPreferredSize().getHeight() / 2);
        }
    }

    /**
     * Draws the component.
     *
     * @param g  Graphics on which to draw.
     */
    @Override
	public final void paint(final Graphics g)
    {
        super.paint(g);

        if ((x1 != 0) && (x2 != 0))
        {
            //DW/2573/BeginPatch
            if ((OrgChartPanel.getDirection() == OrgChartPanel.HORIZONTAL) ||
                    (OrgChartPanel.getDirection() == OrgChartPanel.SEMALIKE))//DW/2573/EndPatch
            {
                g.drawLine(x1, 0, x2, 0);
                g.drawLine(x1, 1, x2, 1);
            }
            else
            {
                g.drawLine(0, x1, 0, x2);
                g.drawLine(1, x1, 1, x2);
            }
        }
    }
}
